package org.apache.ibatis.demo.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @Author：LJ
 * @Description：
 * @Date: 2020/7/7
 * @Modified By:
 */
public class MyInvocationHandler implements InvocationHandler {
  private String theString;

  public MyInvocationHandler (String theString) {
    this.theString = theString;
  }

  @Override
  public Object invoke (Object proxy, Method method, Object[] args) throws Throwable {
    System.out.println("before method call : " + method.getName());
    Object result = method.invoke(theString, args);
    System.out.println("after method call : " + method.getName());
    return result;
  }

  public static void main(String[] args) {
    MyInvocationHandler handler=new MyInvocationHandler("The example string");

    CharSequence sequence=(CharSequence) Proxy.newProxyInstance(MyInvocationHandler.class.getClassLoader(),
      new Class[]{CharSequence.class},handler);

    System.out.println(sequence.length());
    System.out.println(sequence.subSequence(1,3));

  }
}
